using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankWars
{
    public class LastManStandingRules : Rules
    {
        private int nLivesPerPlayer = 0;

        List<Player> lstPlayers = new List<Player>();

        /// <summary>
        /// Returns if the game is over or not
        /// </summary>
        public override bool IsGameEnded
        {
            get
            {
                if (this.lstPlayers.Count == 1)
                {
                    return false;
                }

                int nPlayersRemaing = 0;
                foreach (Player currentPlayer in this.lstPlayers)
                {
                    //If tank isn't dead
                    if (!currentPlayer.Tank.IsDead)
                    {
                        nPlayersRemaing++;

                        //Only need to check to see if more than one player is still alive
                        if (nPlayersRemaing > 1)
                        {
                            //Game not over
                            return false;
                        }
                    }
                }

                //Game finished
                return true;
            }
        }

        public LastManStandingRules(List<Player> players, int livesPerPlayer)
        {
            this.lstPlayers = players;
            this.nLivesPerPlayer = livesPerPlayer;

            foreach (Player currentPlayer in this.lstPlayers)
            {
                currentPlayer.Lives = this.nLivesPerPlayer;
            }
        }

        /// <summary>
        /// Does nothing because game time isn't needed, only lives for each player
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            //Does nothing because it only needs to check number of lives for each player
            //and lives and kills are already handled.
        }

        /// <summary>
        /// Does nothing because there are no extra objects to draw
        /// </summary>
        /// <param name="spriteBatch"></param>
        public override void Draw(SpriteBatch spriteBatch, Map map)
        {
            //Does nothing
        }

        /// <summary>
        /// Checks to see if the given tank still has lives left
        /// </summary>
        /// <param name="tank">Tank to respawn</param>
        /// <returns>true if tank has lives and can respawn</returns>
        public override bool CanTankRespawn(Tank tank)
        {
            if (tank.TankPlayer.Lives > 0)
            {
                return true;
            }

            return false;
        }
    }
}
